# asterisk-menu.pm
# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

# This file is part of the CarrierClass.net project.
# For more information: http://carrierclass.net/
#
package CCNv2::Provision::Asterisk::menu;
use strict;
use warnings;
use base qw(CCNCore::Provision::Base);
use CCN::Text;
use CCNCore::Scripting;
use CCNCore::Home;

sub run()
{
    my $self = shift;
    return unless defined $self->p->{cp_cluster};

    my %menusconf =  ();
    my %menuconf = ();

    # YYY Need to formulate inter-servers menu handling (same as voicemail)
    # before we can do things this way
    #    $self->sql_do_for( <<'SQL', $self->p->{cp_cluster},
    #        SELECT enterprise, pk, dial_cos, default_number, default_cos, default_context FROM menu WHERE cp_cluster = ?
    #SQL
    # This is the same as the 2.0 behavior.
    $self->sql_do_for( <<'SQL', $self->p->{cp_cluster},
        SELECT enterprise, pk, dial_cos, default_number, default_cos, default_context FROM menu 
        WHERE enterprise IN (SELECT enterprise FROM pool WHERE cp_cluster = ?)
SQL
    sub
    {
        my ($ENTERPRISE,$MENU,$dial_cos,$default_number,$default_cos,$default_context) = @_;

        $menusconf{$ENTERPRISE} .= <<"EOT";
exten => menu-${MENU},1,Goto(${ENTERPRISE}-menu-${MENU},s,1)
exten => record-menu-${MENU},1,Goto(${ENTERPRISE}-record-menu-${MENU},s,1)
EOT

        my $home_manager = CCNCore::Home::manager;

        $menuconf{$ENTERPRISE} .= <<"EOT";
;
; menu name: $MENU
;
[${ENTERPRISE}-record-menu-${MENU}]
;
exten => s,1,Answer(2)
exten => s,2,Macro(${ENTERPRISE}-set-context)
exten => s,3,Playback(beep)
exten => s,n,Record(\${SOUNDS}/prompts/${ENTERPRISE}/menu-${MENU}.wav,5)
exten => s,n,exten => s,n,Exec("$home_manager/bin/spool.pl replicate-cp-file \${SOUNDS}/prompts/${ENTERPRISE}/menu-${MENU}.wav")
exten => s,n,Playback(beep)
exten => s,n,Background(prompts/${ENTERPRISE}/menu-${MENU})
exten => s,n,Background(vm-torerecord)
exten => s,n,WaitExten(8)
exten => s,n,Playback(goodbye)
exten => s,n,Hangup
; "Press 3 to re-record it..."
exten => 3,1,Goto(s,3)
exten => i,1,Hangup
exten => t,1,Hangup
;
;
[${ENTERPRISE}-menu-${MENU}]
;
exten => s,1,Answer
exten => s,n,Background(silence/1)
exten => s,n,Goto(s,200)
;
exten => s,200,Background(prompts/${ENTERPRISE}/menu-${MENU})
exten => s,n,DigitTimeout(3)
exten => s,n,ResponseTimeout(10)
exten => s,n,Background(silence/1)
exten => s,n,WaitExten(8)
exten => s,n,Goto(s,200)
;
; "0" in Directory()
exten => o,1,Goto(${ENTERPRISE}-operator,s,1)
; "*" in Directory()
exten => a,1,Goto(s,200)
;
; Invalid choice
exten => i,1,Goto(s,200)
;
EOT

        $menuconf{$ENTERPRISE} .=
           "include => ${ENTERPRISE}-cos-${dial_cos}\n"
                if defined $dial_cos;

        if( defined $default_cos and defined $default_number )
        {
            $menuconf{$ENTERPRISE} .=
                "exten => t,1,Goto(${ENTERPRISE}-$default_cos,$default_number,1)\n";
        }
        else
        {
            $menuconf{$ENTERPRISE} .=
                "exten => t,1,Goto(${ENTERPRISE}-$default_context,s,1)\n"
                    if defined $default_context;
        }

        $self->sql_do_for( <<'SQL', $MENU,
            SELECT "number", "target_number", "target_context", "cos" FROM menu_entry WHERE menu = ?;
SQL
         sub
         {
             my ($number,$target_number,$cos,$target_context) = @_;

             if( defined $cos and defined $target_number )
             {
                 $menuconf{$ENTERPRISE} .=
                     "exten => $number,1,Goto(${ENTERPRISE}-$cos,$target_number,1)\n";
             }
             else
             {
                 $menuconf{$ENTERPRISE} .=
                     "exten => t,1,Goto(${ENTERPRISE}-$target_context,s,1)\n"
                         if defined $target_context;
             }
         });

    });

    for my $ENTERPRISE (keys %menusconf)
    {
        foreach("/etc/asterisk/config/clients/${ENTERPRISE}/extensions.menu.conf")
        {
            CAT $_, "[${ENTERPRISE}-menus]\n".$menusconf{$ENTERPRISE}.$menuconf{$ENTERPRISE};
            IF_CHANGED $_ => CCNv2::Scripting::AST('extensions reload');
        }
    }
}

1;